package com.myWeb.servlet;

import com.myWeb.beans.User;
import com.myWeb.dao.IUserDao;
import com.myWeb.dao.impl.userDao;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class RegisterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        System.out.println("registerServlet");
        //获取表单中的属性值
        String username = req.getParameter("username");
        String password = req.getParameter("userpwd");
        String email = req.getParameter("useremail");
        String phone = req.getParameter("userphone");
        PrintWriter out = resp.getWriter();
        IUserDao userDao = new userDao();
        User user = new User();
        user.setName(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setTelephone(phone);
        try {
            userDao.saveUser(user);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        req.getRequestDispatcher("login.jsp").forward(req,resp);
        out.close();
    }
}

